package com.starbaba.mall.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.starbaba.base.dao.LoanManagerBaseDao;
import com.starbaba.mall.service.MallItemTopicService;
import com.tuanzi.server.loan_base.model.db.Loan_Mall_Item_Topic;

public class MallItemTopicServiceImpl extends LoanManagerBaseDao implements MallItemTopicService{

	@Override
	public void addTopicItem(Map<String, Object> paramsMap) {
		
		String topic_id = (String) paramsMap.get("topic_id");
		String item_source_id = (String) paramsMap.get("item_source_id");
		String sort_index = (String) paramsMap.get("sort_index");
		
		Loan_Mall_Item_Topic itemTopic = new Loan_Mall_Item_Topic();
		itemTopic.setTopic_id(Long.parseLong(topic_id));
		itemTopic.setItem_source_id(item_source_id);
		itemTopic.setSort_index(Integer.parseInt(sort_index));
		
		try {
			getDao().add(itemTopic);
		} catch (AppException e) {
			LogService.error(e);
		}
		
	}
	

	@Override
	public List<Loan_Mall_Item_Topic> getLoan_Mall_Item(Long topic_id,int pageNo, int pageSize){
		String sql = "SELECT t.`item_source_id`,t.`sort_index`,i.`title` AS item_title,i.`status` AS item_status ,"
				+ "i.is_coupon_item "
				+ "FROM `loan_mall_item_topic` t INNER JOIN `loan_mall_item` i ON t.`item_source_id` = i.`source_id`  "
				+ "WHERE topic_id = ? ORDER BY sort_index LIMIT ?,? ";
		List<Loan_Mall_Item_Topic> list = new ArrayList<Loan_Mall_Item_Topic>();
		try {
			list = getDao().list(sql, Loan_Mall_Item_Topic.class, topic_id, pageNo, pageSize);
		} catch (AppException e) {
			LogService.error(e);
		}
		return list;
	}

	@Override
	public int countLoan_Mall_Item(Long topic_id) {
		String sql = "select count(*) from `loan_mall_item_topic` where topic_id = ? "; 
		int result = 0;
		try {
			result = getDao().getInt(sql, topic_id);
		} catch (AppException e) {
			LogService.error(e);
		}
		return result;
	}
	
	@Override
	public String getSelectItemSourceIds(Long topic_id) {
		
		String sql = "SELECT t.`item_source_id`,t.`sort_index`,i.`title` AS item_title,i.`status` AS item_status "
				+ "FROM `loan_mall_item_topic` t INNER JOIN `loan_mall_item` i ON t.`item_source_id` = i.`source_id`  "
				+ "WHERE topic_id = ? ORDER BY sort_index ";
		String source_ids = "";
		try {
			List<Loan_Mall_Item_Topic> list = getDao().list(sql, Loan_Mall_Item_Topic.class, topic_id);
			for(Loan_Mall_Item_Topic itemTopic: list){
				String source_id = itemTopic.getItem_source_id();
				source_ids  = source_ids + source_id + ",";
			}
		} catch (AppException e) {
			LogService.error(e);
		}
		
		return source_ids;
	}

	@Override
	public int deleteLoan_Mall_Item(Long topic_id, String item_source_id) {
		String sql = "DELETE FROM `loan_mall_item_topic` WHERE topic_id = ? AND item_source_id = ?";
		int result = 0;
		try {
			result = getDao().delete(sql, topic_id, item_source_id);
		} catch (AppException e) {
			LogService.error(e);
		}
		return result;
	}

	@Override
	public void sortTab(Map<String, Object> paramsMap) {
		String topic_id = (String) paramsMap.get("topic_id");
		String prdidListStr = (String) paramsMap.get("prdSortList");
		String[] prdidList = prdidListStr.split("#");
		String sql = "UPDATE `loan_mall_item_topic` SET sort_index = ? WHERE topic_id = ? AND item_source_id = ? ";
		for (int index = 0; index < prdidList.length; ++index) {
			Long item_source_id = Long.parseLong(prdidList[index]);
			try {
				int result = getDao().update(sql, index, topic_id, item_source_id);
			} catch (AppException e) {
				LogService.error(e);
			}
		}
		
	}

	@Override
	public int deleteLoan_Mall_Item(Long topic_id) {
		String sql = "DELETE FROM `loan_mall_item_topic` WHERE topic_id = ? ";
		int result = 0;
		try {
			result = getDao().delete(sql, topic_id);
		} catch (AppException e) {
			LogService.error(e);
		}
		return result;
	}



}
